<?php

require __DIR__.'/../vendor/autoload.php';

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

class consumer
{
    /** @var  AMQPStreamConnection*/
    public $connect = null;

    private $que = 'hello';

    private $vhost = 'MemberHost';

    public function __construct()
    {
        $this->conn();
    }

    public function conn()
    {
        $host = '192.168.3.203';
        $port = '5672';
        $user = 'MemberAdmin';
        $pwd = '123456';
        $this->connect = new AMQPStreamConnection($host, $port, $user, $pwd,$this->vhost);
    }

    public function recive()
    {
        $channel = $this->connect->channel();
        $channel->queue_declare($this->que, false, false, false, false);
        echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";
        $callback = function(AMQPMessage $msg) {
            try {
                throw new \Exception('消费失败');

            } catch (\Exception $e) {
                echo $e->getMessage().PHP_EOL;
                $msg->nack(true);
            }
            //echo " [x] Received ", $msg->body, "\n";
        };
        $channel->basic_consume($this->que, '', false, false, false, false, $callback);
        while(count($channel->callbacks)) {
            $channel->wait();
        }

        $channel->close();
        $this->connect->close();
        echo '信息发送成功'.PHP_EOL;
    }
}

(new consumer)->recive();
